package root.electronclass.electronclass.common.util;

import android.os.Handler;
import android.os.Message;
import android.util.Log;


import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;

import root.kaer.serialport.SerialPort;


//处理刷卡的业务
public class KRReadThreadUtil {
    private final String          TAG        = "ReadThreadUtil";
    static SerialPort sp;
    static        FileInputStream mInputStream;
    private       ReadThread      mReadThread;
    private       readListener    listener;
    private final byte[]          headArrPre = new byte[]{(byte) 0x02};
    private final byte[]          endArrPre  = new byte[]{(byte) 0x0D, (byte) 0x0A, (byte) 0x03};

    public void startReadThread(readListener listener) {
        mReadThread = new ReadThread();
        mReadThread.start();
        this.listener = listener;
    }

    public void stopReadThread() {
        if (mReadThread != null)
            mReadThread.interrupt();
    }


    public interface readListener {
        void onRead(int type, String cardNum);
    }

    class ReadThread extends Thread {
        public void run() {
            super.run();
            try {
                //设置串口号、波特率
                String serial_port = "ttyS3";
                sp = new SerialPort(new File("/dev/" + serial_port).getAbsolutePath(),
                        115200);
                Log.i(TAG, "当前的串口为--->" + "/dev/" + serial_port);

                mInputStream = (FileInputStream) sp.getInputStream();
                while (!isInterrupted()) {
                    int    size;
                    byte[] buffer = new byte[64];
                    if (mInputStream == null)
                        return;
                    int count = 0;
                    while (count == 0) {
                        count = mInputStream.available();
                    }
                    if (mInputStream.available() >= 7) {
                        size = mInputStream.read(buffer);
                        if (size > 0) {
                            onDataReceived(buffer, size);
                        }
                    }
                    Thread.sleep(10);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
        }
    }

    private void onDataReceived(final byte[] buffer, final int size) {
        try {
            String carNum = "";
            if (Arrays.equals(Arrays.copyOfRange(buffer, 0, 1), headArrPre) && Arrays.equals(Arrays.copyOfRange(buffer, size - 3, size), endArrPre)) {
                byte[] tmp     = Arrays.copyOfRange(buffer, 1, size - 3);
                String card    = new String(tmp, "ASCII").toUpperCase();
                long   dec_num = Long.parseLong(card, 16);
                carNum = String.valueOf(dec_num);
                Log.i(TAG, "读卡数据2--->" + carNum);
                Message msg = new Message();
                msg.what = 1;
                msg.obj = StringUitl.addZeroForNum(carNum,10);
                mHandler.sendMessage(msg); // 向Handler发送消息,更新UI
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    //获取传递的数据
                    String card = (String) msg.obj;
                    Log.i(TAG, "读卡数据3--->" + card);
                    listener.onRead(0, card);
            }
        }

        ;
    };


}




